#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "dhnetsdk.h"

static BOOL SDKInitFlag = FALSE;
static LLONG g_lLoginHandle = 0;
static LLONG g_LRealLoadHandle = 0;
static LLONG g_LRealLoadHandle1 = 0;
static char g_szDevIp[32] = "112.80.123.205";
static WORD g_nPort = 37777;
static char g_szUserName[64] = "admin";
static char g_szPasswd[64] = "wwzy123456";

void CALLBACK DisConnectFunc(LLONG lLoginID, char *pchDVRIP, LONG nDVRPort, LDWORD dwUser);
void CALLBACK HaveReConnect(LLONG lLoginID, char *pchDVRIP, LONG nDVRPort, LDWORD dwUser);
int CALLBACK AnalyzerDataCallBack(LLONG lAnalyzerHandle, DWORD dwAlarmType, void* pAlarmInfo, BYTE *pBuffer, DWORD dwBufSize, LDWORD dwUser, int nSequence, void *reserved);
int CALLBACK AnalyzerDataCallBack1(LLONG lAnalyzerHandle, DWORD dwAlarmType, void* pAlarmInfo, BYTE *pBuffer, DWORD dwBufSize, LDWORD dwUser, int nSequence, void *reserved);

void InitTest()
{
	SDKInitFlag = CLIENT_Init(DisConnectFunc,0);
	if (SDKInitFlag == FALSE)
	{
		printf("Initialize SDK fail !\n");
	}
	else
	{
		printf("Initialize SDK succeed !\n");
	}
	
	CLIENT_SetAutoReconnect(&HaveReConnect,0);
	
	NET_DEVICEINFO_Ex stDevInfoEx = {0};
	int nError = 0;
	while (g_lLoginHandle == 0)
	{
		g_lLoginHandle = CLIENT_LoginEx2(g_szDevIp,g_nPort,g_szUserName,g_szPasswd,EM_LOGIN_SPEC_CAP_TCP,NULL,&stDevInfoEx,&nError);
		if (g_lLoginHandle == 0)
		{
			printf("CLIENT_LoginEx2 %s [%d] Failed! Last error[%x]\n",g_szDevIp,g_nPort,CLIENT_GetLastError());
		}
		else
		{
			printf("CLIENT_LoginEx2 %s [%d] Succeed! \n",g_szDevIp,g_nPort);
		}
	}
	
	sleep(1);
	printf("\n");
}

void RunTest()
{
	if (SDKInitFlag == FALSE)
	{
		return ;
	}
	if (g_lLoginHandle == 0)
	{
		return ;
	}

	LDWORD dwUser = 0;
	int nChannel = 0;	
	g_LRealLoadHandle = CLIENT_RealLoadPictureEx(g_lLoginHandle,nChannel,EVENT_IVS_ALL,FALSE,AnalyzerDataCallBack,dwUser,NULL);
	
	int nChannel1 = 1;	
	g_LRealLoadHandle1 = CLIENT_RealLoadPictureEx(g_lLoginHandle,nChannel1,EVENT_IVS_ALL,FALSE,AnalyzerDataCallBack1,dwUser,NULL);
	
	if (g_LRealLoadHandle == 0 || g_LRealLoadHandle1 == 0)
	{
		printf("CLIENT_RealLoadPictureEx Failed ! Last Error [%x]\n",CLIENT_GetLastError());
		return ;
	}
	else
	{
		printf("CLIENT_RealLoadPictureEx Succeed!\n");
	}
}

void EndTest()
{
	printf ("input any key to quit!\n");
	getchar();
	
	//stop the load picture alarm
	if (g_LRealLoadHandle != 0 && g_LRealLoadHandle1 != 0)
	{
		if (CLIENT_StopLoadPic(g_LRealLoadHandle) == FALSE || CLIENT_StopLoadPic(g_LRealLoadHandle1) == FALSE)
		{
			printf("CLIENT_StopLoadPic Failed! Last Error [%x]\n",CLIENT_GetLastError());
		}
		else
		{
			printf("CLIENT_StopLoadPic Succeed!\n");
			g_LRealLoadHandle = 0;
			g_LRealLoadHandle1 = 0;
		}
	}
	
	//log out the facility
	if (g_lLoginHandle != 0)
	{
		if (CLIENT_Logout(g_lLoginHandle) == FALSE)
		{
			printf("CLIENT_Logout Failed! Last Error [%x]\n",CLIENT_GetLastError());
		}
		else
		{
			printf("CLIENT_Logout Succeed!\n");
			g_lLoginHandle = 0;
		}
	}
	
	//clear init resource
	if (SDKInitFlag == TRUE)
	{
		CLIENT_Cleanup();
		SDKInitFlag = FALSE;
		printf("CLIENT_Cleanup Succeed!\n");
	}
	return;
}

int main ()
{
	InitTest();
	RunTest();
	EndTest();
	return 0;
}

void CALLBACK DisConnectFunc(LLONG lLoginID, char *pchDVRIP, LONG nDVRPort, LDWORD dwUser)
{
	printf("Call DisConnectFunc\n");
	printf("lLoginID[0x%x]\n",lLoginID);
	if (pchDVRIP != NULL)
	{
		printf("pchDVRIP[%s]\n",pchDVRIP);
	}
	printf("nDVRPort[%d]\n",nDVRPort);
	printf("dwUser[%p]\n",dwUser);
	printf("\n");
	
}

void CALLBACK HaveReConnect(LLONG lLoginID, char *pchDVRIP, LONG nDVRPort, LDWORD dwUser)
{
	printf("Call HaveReConnect\n");
	printf("lLoginID[0x%x]\n",lLoginID);
	if (pchDVRIP != NULL)
	{
		printf("pchDVRIP[%s]\n",pchDVRIP);
	}
	printf("nDVRPort[%d]\n",nDVRPort);
	printf("dwUser[%p]\n",dwUser);
	printf("\n");
}

int CALLBACK AnalyzerDataCallBack(LLONG lAnalyzerHandle, DWORD dwAlarmType, void* pAlarmInfo, BYTE *pBuffer, DWORD dwBufSize, LDWORD dwUser, int nSequence, void *reserved)
{
	if (lAnalyzerHandle != g_LRealLoadHandle)
	{
		return 0;
	}
	
	{
		int nAlarmChn = 0;
		int nnLane = 0; 
		int nnParkingSpaceStatus = 0;
		DWORD  nPTS = 0;
		char CarNumber[32] = {};
		//NET_TIME_EX nUTC ; 
		DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO nstTrafficCar; 
		switch(dwAlarmType)
		{
			case EVENT_IVS_TRAFFIC_PARKINGSPACEPARKING:
				{
					printf("**************HaveCar Event!**************\n");
					DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO *pStuInfo = (DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO*)pAlarmInfo;
					
					nAlarmChn = pStuInfo->nChannelID;
					nnLane = pStuInfo->nLane;
					nPTS = pStuInfo->PTS;
					//nUTC = pStuInfo->UTC;
					nstTrafficCar = pStuInfo->stTrafficCar;
					memcpy (CarNumber,nstTrafficCar.szPlateNumber,32);
					nnParkingSpaceStatus = pStuInfo->nParkingSpaceStatus; 
					
					printf("ChannelID-CarLocation[%d-%d]\n",nAlarmChn,nnLane);
					printf("FindTime[%d]\n",nPTS);
					printf("CarNumber[%s]\n",CarNumber);
					printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
					//printf("ChannelID-CarLocation[%d-%d]\n",pStuInfo->nChannelID,pStuInfo->nLane);
					//printf("FindTime[%d.%d.%d,%d:%d:%d]\n",nUTC.dwYear,nUTC.dwMonth,nUTC.dwDay,nUTC.dwHour,nUTC.dwMinute,nUTC.dwSecond);
					//printf("CarNumber[%s]\n",nstTrafficCar.szPlateNumber);
					//printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
				}
				break;
			
			case EVENT_IVS_TRAFFIC_PARKINGSPACENOPARKING:
				{
					printf("**************NoCar Event!**************\n");
					DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO *pStuInfo = (DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO*)pAlarmInfo;
					
					nAlarmChn = pStuInfo->nChannelID;
					nnLane = pStuInfo->nLane;
					nPTS = pStuInfo->PTS;
					//nUTC = pStuInfo->UTC;
					nstTrafficCar = pStuInfo->stTrafficCar;
					memcpy (CarNumber,nstTrafficCar.szPlateNumber,32);
					nnParkingSpaceStatus = pStuInfo->nParkingSpaceStatus; 
					
					printf("ChannelID-CarLocation[%d-%d]\n",nAlarmChn,nnLane);
					printf("FindTime[%d]\n",nPTS);
					printf("CarNumber[%s]\n",CarNumber);
					printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
					//printf("ChannelID-CarLocation[%d-%d]\n",pStuInfo->nChannelID,pStuInfo->nLane);
					//printf("FindTime[%d.%d.%d,%d:%d:%d]\n",nUTC.dwYear,nUTC.dwMonth,nUTC.dwDay,nUTC.dwHour,nUTC.dwMinute,nUTC.dwSecond);
					//printf("CarNumber[%s]\n",nstTrafficCar.szPlateNumber);
					//printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
                                }
				break;
			
			default:
				printf("other event type [%d]\n",dwAlarmType);	
				break;
		}
	}
}

int CALLBACK AnalyzerDataCallBack1(LLONG lAnalyzerHandle, DWORD dwAlarmType, void* pAlarmInfo, BYTE *pBuffer, DWORD dwBufSize, LDWORD dwUser, int nSequence, void *reserved)
{
	if (lAnalyzerHandle != g_LRealLoadHandle1)
	{
		return 0;
	}
	
	{
		int nAlarmChn = 0;
		int nnLane = 0; 
		int nnParkingSpaceStatus = 0;
		DWORD  nPTS = 0;
		char CarNumber[32] = {};
		//NET_TIME_EX nUTC ; 
		DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO nstTrafficCar; 
		switch(dwAlarmType)
		{
			case EVENT_IVS_TRAFFIC_PARKINGSPACEPARKING:
				{
					printf("**************HaveCar Event!**************\n");
					DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO *pStuInfo = (DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO*)pAlarmInfo;
					
					nAlarmChn = pStuInfo->nChannelID;
					nnLane = pStuInfo->nLane;
					nPTS = pStuInfo->PTS;
					//nUTC = pStuInfo->UTC;
					nstTrafficCar = pStuInfo->stTrafficCar;
					memcpy (CarNumber,nstTrafficCar.szPlateNumber,32);
					nnParkingSpaceStatus = pStuInfo->nParkingSpaceStatus; 
					
					printf("ChannelID-CarLocation[%d-%d]\n",nAlarmChn,nnLane);
					printf("FindTime[%d]\n",nPTS);
					printf("CarNumber[%s]\n",CarNumber);
					printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
					//printf("ChannelID-CarLocation[%d-%d]\n",pStuInfo->nChannelID,pStuInfo->nLane);
					//printf("FindTime[%d.%d.%d,%d:%d:%d]\n",nUTC.dwYear,nUTC.dwMonth,nUTC.dwDay,nUTC.dwHour,nUTC.dwMinute,nUTC.dwSecond);
					//printf("CarNumber[%s]\n",nstTrafficCar.szPlateNumber);
					//printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
				}
				break;
			
			case EVENT_IVS_TRAFFIC_PARKINGSPACENOPARKING:
				{
					printf("**************NoCar Event!**************\n");
					DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO *pStuInfo = (DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO*)pAlarmInfo;
					
					nAlarmChn = pStuInfo->nChannelID;
					nnLane = pStuInfo->nLane;
					nPTS = pStuInfo->PTS;
					//nUTC = pStuInfo->UTC;
					nstTrafficCar = pStuInfo->stTrafficCar;
					memcpy (CarNumber,nstTrafficCar.szPlateNumber,32);
					nnParkingSpaceStatus = pStuInfo->nParkingSpaceStatus; 
					
					printf("ChannelID-CarLocation[%d-%d]\n",nAlarmChn,nnLane);
					printf("FindTime[%d]\n",nPTS);
					printf("CarNumber[%s]\n",CarNumber);
					printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
					//printf("ChannelID-CarLocation[%d-%d]\n",pStuInfo->nChannelID,pStuInfo->nLane);
					//printf("FindTime[%d.%d.%d,%d:%d:%d]\n",nUTC.dwYear,nUTC.dwMonth,nUTC.dwDay,nUTC.dwHour,nUTC.dwMinute,nUTC.dwSecond);
					//printf("CarNumber[%s]\n",nstTrafficCar.szPlateNumber);
					//printf("ParkSpaceStatus[%d]\n",nnParkingSpaceStatus);
                                }
				break;
			
			default:
				printf("other event type [%d]\n",dwAlarmType);	
				break;
		}
	}
}







